package tiles;

import java.util.ArrayList;
import java.util.HashMap;

import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Mesh.Mode;

import utilities.CoordConvert;
import utilities.CustomMesh;
import utilities.MVCMaker;
import view.EViewList;
import view.View;

import controller.AbstractController;
import eventManager.EventManager;
import gameAppLayer.enums.Constants;

/*
 * Creates tile perimeters and centers
 * Input:  ArrayList of locations (hex coords)
 * Output:  List of controllers for tiles, single perimeter object
 */
public class GlobalHexTilesBuilder {
	
	private EventManager eventManager;
	private AssetManager assetManager;
	
	public GlobalHexTilesBuilder(EventManager eventManager, AssetManager assetManager) {
		this.eventManager = eventManager;
		this.assetManager = assetManager;
	}
	
	public ArrayList<AbstractController> buildTiles(ArrayList<ArrayList<Integer>> locations) {
		
		ArrayList<AbstractController> controllers = new ArrayList<AbstractController>();
		
		int u, v, w;		
		HexTileModel hexTile;
		HashMap<EViewList, View> hexViews;
		HexTileController hexControl;
		HexTileViewBuilder hexViewFact = new HexTileViewBuilder();
		
		int numberHexes = locations.size();
		int vertsPerHex = 12; //6 lines * 2 verts per line
		ArrayList<Double> cartCoords;
		CustomMesh lineMesh = new CustomMesh(numberHexes*vertsPerHex);
		lineMesh.setMode(Mode.Lines);
		float centerX, centerY; //center location of a hexGrid (cartesian coords)
		float xVal = Constants.hexXVal;
		float yVal = Constants.hexYVal;
		
		for (ArrayList<Integer> location: locations) {			
			//create tile interiors			
			u = (int) location.get(0); 
			v = (int) location.get(1); 
			w = (int) location.get(2); 	
			
			hexTile = new HexTileModel(u,v,w);
			hexViews = hexViewFact.buildViews(assetManager);
			hexControl = new HexTileController(eventManager);
			
			MVCMaker.linkElements(hexTile, hexViews, hexControl);
			controllers.add(hexControl);
			
			
			//create tile perimeters (one big line mesh)
			cartCoords = CoordConvert.hex2Cart(location);
			centerX = cartCoords.get(0).floatValue();
			centerY = cartCoords.get(1).floatValue();
			
			lineMesh.addVertex(centerX-xVal,centerY-yVal/2,0);
			lineMesh.addVertex(centerX,centerY-yVal,0);
			
			lineMesh.addVertex(centerX,centerY-yVal,0);
			lineMesh.addVertex(centerX+xVal,centerY-yVal/2,0);
			
			lineMesh.addVertex(centerX+xVal,centerY-yVal/2,0);
			lineMesh.addVertex(centerX+xVal,centerY+yVal/2,0);
			
			lineMesh.addVertex(centerX+xVal,centerY+yVal/2,0);
			lineMesh.addVertex(centerX,centerY+yVal,0);
			
			lineMesh.addVertex(centerX,centerY+yVal,0);
			lineMesh.addVertex(centerX-xVal,centerY+yVal/2,0);
			
			lineMesh.addVertex(centerX-xVal,centerY+yVal/2,0);
			lineMesh.addVertex(centerX-xVal,centerY-yVal/2,0);			
		}
		
		lineMesh.setLineWidth(3.0f);
		lineMesh.finish();
		lineMesh.setStatic();
		
		Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
		mat.setColor("Color", ColorRGBA.Blue);
		mat.setColor("GlowColor", ColorRGBA.Blue);
		
		//CREATE VHexPerim object
		//Default view
		HexPerimView hexPerimView = new HexPerimView(EViewList.DEFAULT, mat, lineMesh);
		HexPerimController hexPerimController = new HexPerimController(eventManager);
		
		hexPerimController.setSpatial(hexPerimView);
		hexPerimView.setController(hexPerimController);
		hexPerimView.initView();
		
		controllers.add(hexPerimController);
		
		return controllers;
	}
	

}
